﻿//-----------------------------------------------------------------------
// <copyright file="Repository.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: Repository.cs
// * history : created by qinchaoyue 2017-10-24 11:31:05
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Slwy.Data.Entity.Interfaces;
using Slwy.Logger.Contracts;
using SqlSugar;
using Slwy.MySql.Contracts;

namespace Slwy.Data.Entity
{
    /// <summary>
    /// The repository.
    /// </summary>
    /// <typeparam name="TEntity">
    /// </typeparam>
    /// <typeparam name="TKey">
    /// </typeparam>
    public class Repository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
        where TEntity : class, IEntityBase<TKey>, new()
    {
        private readonly ILogger _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="Repository{TEntity,TKey}"/> class.
        /// </summary>
        /// <param name="logger">日志</param>
        public Repository(ILogger logger, IUnitOfWork unitOfWork)
            :base(logger, unitOfWork)
        {
            this._logger = logger;
            // this.Init();
        }

        /// <summary>
        /// 初始化
        /// 修改类和表的映射关系
        /// </summary>
        //private void Init()
        //{
        //    // var typeName = typeof(TEntity).Name;
        //    // var tableName = typeName.Replace("Entity", string.Empty);
        //    // base.Client.MappingTables.Add(typeName, tableName);
        //}

        public TEntity GetByKey(TKey key)
        {
            return this.Client.Queryable<TEntity>().InSingle(key);
        }

        public IUpdateable<TEntity> Update(IList<TEntity> entities)
        {
            return this.Client.Updateable<TEntity>(entities);
        }

        public IUpdateable<TEntity> Update(TEntity entity)
        {
            return this.Client.Updateable(entity);
        }
    }
}
